package cn.scw.project.common;

import cn.scw.common.constant.SysConsts;
import cn.scw.project.separate.domain.SaUser;
import cn.scw.project.separate.service.SaUserService;
import cn.scw.project.system.service.ISysPayService;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

/**
 * 打印机广告
 *
 * @author api
 */
@RestController
@RequestMapping(SysConsts.CONTEXT_PATH)
public class WxController {

    @Resource
    private ISysPayService iSysPayService;

    @Resource
    private SaUserService saUserService;

    /**
     * 随行付支付回调
     *
     * @param request
     */
    @RequestMapping("/notify")
    public String notify(HttpServletRequest request) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        Enumeration<String> enumeration = request.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String paramsKey = enumeration.nextElement();
            params.put(paramsKey, request.getParameter(paramsKey));
        }
        System.out.println("================================================随行付接口 params 回调=======================================================");
        System.out.println(JSONObject.toJSONString(params));
        System.out.println("================================================随行付接口 params 回调=======================================================");
        return iSysPayService.payBack(params);
    }


    /**
     * 随行付商户注册回调
     *
     * @param request
     */
    @RequestMapping("/register")
    public String register(HttpServletRequest request) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        Enumeration<String> enumeration = request.getParameterNames();
        while (enumeration.hasMoreElements()) {
            String paramsKey = enumeration.nextElement();
            params.put(paramsKey, request.getParameter(paramsKey));
        }
        System.out.println("================================================随行付商户注册 params 回调=======================================================");
        System.out.println(JSONObject.toJSONString(params));
        System.out.println("================================================随行付商户注册 params 回调=======================================================");
        Long id = Long.valueOf(params.get("reqNo").split("_")[0]);
        SaUser saUser = saUserService.select(id);
        if (saUser != null) {
            saUser.setErrMsg(params.get("retMsg"));
            saUser.setMercNo(params.get("merchantNo"));
            if (Integer.valueOf(params.get("status")).compareTo(2) == 0) {
                saUser.setState(1);
            } else if (Integer.valueOf(params.get("status")).compareTo(3) == 0) {
                saUser.setState(3);
            }
            saUserService.save(saUser);
        }
        return "SUCCESS";
    }
}
